package com.js.myapp.system.activity;

// Created by JS on 2021/5/14.

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.js.basehttp.common.ResponseObserver;
import com.js.basehttp.utils.RxUtil;
import com.js.basicutils.LogUtils;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.StatusBarUtil;
import com.js.myapp.R;
import com.js.myapp.databinding.ActivityNetWorkDataBinding;
import com.js.myapp.network.RetrofitHelper;
import com.js.myapp.network.request.ArticleWrapper;
import com.js.myapp.network.response.LoginResponse;

import java.util.HashMap;
import java.util.Map;

public class NetWorkDataActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "NetWorkDataActivity";

    private NetWorkDataActivity mContext;
    private ActivityNetWorkDataBinding binding;

    public static void start(Context context) {
        Intent intent = new Intent(context, NetWorkDataActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mContext = this;
        NavigationBarUtil.setSysNavigationColor(this, Color.parseColor("#141d26"));
        StatusBarUtil.setStatusBarColor(this, R.color.color_statusbar_bg);
        binding = ActivityNetWorkDataBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.btnLogin.setOnClickListener(this);
        binding.btnGetData.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_get_data:
                getData();
                break;
        }
    }

    private void login() {
        LogUtils.d("NetWorkData", "login");
        RetrofitHelper.getApiService()
                .login(getParameters())
                .compose(RxUtil.rxSchedulerHelper(mContext, true))
                .subscribe(new ResponseObserver<LoginResponse>() {
                    @Override
                    public void onSuccess(LoginResponse response) {
                        showToast("登录成功~");
                    }
                });
    }

    private Map<String, Object> getParameters() {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "110120");
        map.put("password", "123456");
        return map;
    }

    private void getData() {
        RetrofitHelper.getApiService()
                .getArticle()
                .compose(RxUtil.rxSchedulerHelper(this, true))
                .subscribe(new ResponseObserver<ArticleWrapper>() {
                    @Override
                    public void onSuccess(ArticleWrapper response) {
                        showToast("获取的文章：" + response.getDatas().size());
                        LogUtils.d(TAG, "第一篇文章：" + response.getDatas().get(0).toString());
                    }
                });
    }
}
